﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using System.Text;

namespace sBlog.Net.Upload
{
    public static class clsFunction
    {
        public static void FtpCreateFolder(string strFolder, string ftpUser, string ftpPassword)
        {
            try
            {
                FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(strFolder);
                ftp.Method = WebRequestMethods.Ftp.MakeDirectory;
                ftp.Credentials = new NetworkCredential(ftpUser, ftpPassword);
                ftp.UsePassive = false;
                FtpWebResponse CreateForderResponse = (FtpWebResponse)ftp.GetResponse();
            }
            catch (Exception ex)
            {
                //Log("Error FtpCreateFolder: " + ex.Message + "---" + strFolder + "---" + ftpUser );
            }
        }

        public static void UpFileServer(string strUrl, Byte[] buffer)
        {
            try
            {
                FtpWebRequest requestObj = (FtpWebRequest)FtpWebRequest.Create(strUrl);
                requestObj.Method = WebRequestMethods.Ftp.UploadFile;
                requestObj.UseBinary = true;
                requestObj.KeepAlive = false;
                requestObj.Credentials = new NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["ftpUserName"].ToString(), System.Configuration.ConfigurationManager.AppSettings["ftpPassword"].ToString());
                Stream requestStream = requestObj.GetRequestStream();
                requestStream.Write(buffer, 0, buffer.Length);

                requestStream.Flush();
                requestStream.Close();
                requestObj = null;
            }
            catch (Exception ex)
            {
                Log("UpFileServer:" + ex.Message);
            }
        }

        public static void UploadVideoClip(string strUrl, HttpPostedFile files)
        {
            try
            {
                Stream objStream = files.InputStream;
                Byte[] buffer = new Byte[files.ContentLength];
                objStream.Read(buffer, 0, buffer.Length);
                objStream.Close();
                objStream = null;

                FtpWebRequest requestObj = (FtpWebRequest)FtpWebRequest.Create(strUrl);
                requestObj.Method = WebRequestMethods.Ftp.UploadFile;

                requestObj.UseBinary = true;
                requestObj.Credentials = new NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["ftpUserName"].ToString(), System.Configuration.ConfigurationManager.AppSettings["ftpPassword"].ToString());
                requestObj.KeepAlive = false;

                Stream ReqStream = requestObj.GetRequestStream();
                ReqStream.Write(buffer, 0, buffer.Length);
                ReqStream.Flush();
                ReqStream.Close();
                requestObj = null;
            }
            catch (Exception ex)
            {
                Log("UploadVideoClip:" + ex.Message);
            }
        }

        public static void CreateUserFolder(string strRoot, string strUserId, string strFtpUser, string strFtpPass)
        {
            string strTmp = GetRootFolder(strRoot, strUserId);
            FtpCreateFolder(strTmp, strFtpUser, strFtpPass);
            strTmp = GetFullFolder(strTmp);
            FtpCreateFolder(strTmp, strFtpUser, strFtpPass);
        }

        public static string GetFolderUser(string strRoot, string strUserId)
        {
            string strTmp = GetRootFolder(strRoot, strUserId);
            return GetFullFolder(strTmp);
        }

        public static string GetRootFolder(string strRoot, string strUserId)
        {
            if (strRoot.Substring(strRoot.Length - 1) != "/")
                strRoot += "/" + strUserId;
            else
                strRoot += strUserId;
            return strRoot;
        }

        public static string GetFullFolder(string strRoot)
        {
            string strDate = DateTime.Now.Year.ToString() + DateTime.Now.ToString("MM");
            strRoot += "/" + strDate;
            return strRoot;
        }

        public static string GetFolderByDate(string strRoot)
        {
            if (strRoot.Substring(strRoot.Length - 1) != "/")
                strRoot += "/";
            return GetFullFolder(strRoot);
        }

        public static void Log(string error)
        {
            System.IO.DirectoryInfo diLog = new System.IO.DirectoryInfo(System.Web.HttpRuntime.AppDomainAppPath.ToString() + "/Logs/");
            if (!diLog.Exists)
                diLog.Create();

            Logger.FileLog logs = new Logger.FileLog(System.Web.HttpRuntime.AppDomainAppPath.ToString() + "/Logs/", "Mlog2013.log");
            logs.genericWrite(error);
            logs.finalize();
        }

        public static void Info(string lbinfo)
        {
            System.IO.DirectoryInfo diLog = new System.IO.DirectoryInfo(System.Web.HttpRuntime.AppDomainAppPath.ToString() + "/Logs/");
            if (!diLog.Exists)
                diLog.Create();

            Logger.FileLog logs = new Logger.FileLog(System.Web.HttpRuntime.AppDomainAppPath.ToString() + "/Logs/", "LogIn.log");
            logs.genericWrite(lbinfo);
            logs.finalize();
        }
    }
}